<template>
  <!-- <div class='container'>添加文章对话框</div> -->
  <el-dialog :title="form.id?'修改文章':'新增文章'" :visible="showAdd" @close="onClose">
    <el-form ref="formRef" :model="form" :rules="rules" size="medium" label-width="80px">
      <el-form-item label="文章标题">
        <el-input v-model="form.title" autocomplete="off" :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="文章内容">
        <!-- <div class="local-quill-editor"> -->
        <quill-editor
          ref="myLQuillEditor"
          v-model="form.articleBody"
          :options="editorOption"
          class="editor"
        >
          >
        </quill-editor>
        <!-- </div> -->
      </el-form-item>
      <el-form-item label="视频地址">
        <el-input v-model="form.videoURL" autocomplete="off" :style="{width: '100%'}"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取消</el-button>
      <el-button type="primary" @click="handelConfirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, update } from '@/api/hmmm/articles.js'
// 工具栏配置项
const toolbarOptions = [
  // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
  ['bold', 'italic', 'underline', 'strike'],
  // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
  [{ list: 'ordered' }, { list: 'bullet' }],
  // 引用  代码块-----['blockquote', 'code-block']
  ['blockquote'],
  ['code-block'],
  // 链接、图片、视频-----['link', 'image', 'video']
  ['image', 'link']
]
export default {
  name: 'AddPage',
  components: {},
  props: {
    showAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      editorOption: {
        modules: {
          toolbar: toolbarOptions
        },
        theme: 'snow',
        placeholder: ''
        // Some Quill optiosn...
      },
      rules: {
        title: [{
          required: true,
          message: '请输入文章标题',
          trigger: 'blur'
        }],
        editorOption: [{
          required: true,
          message: '请输入文章标题',
          trigger: 'blur'
        }],
        videoURL: [{
          required: false,
          trigger: 'blur'
        }]
      }
    }
  },

  methods: {
    onClose() {
      this.$refs['formRef'].resetFields()
      this.form = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
      this.$emit('update:showAdd', false)
    },
    handelConfirm() {
      this.$refs['formRef'].validate(async valid => {
        if (!valid) return
        // 提示
        if (this.form.id) {
        // 编辑
          await update(this.form)
        } else {
        // 新增
          await add(this.form)
        }
        this.$message({
          message: '新增文章成功',
          type: 'success',
          showClose: true,
          duration: 3000
        })
        // 刷新页面
        // this.$emit('success') // 方式1
        this.$parent.getList() // 方式2，this.$parent代表父组件
        this.onClose()
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.el-dialog{
  z-index: 9;
  width: 800px;
}
.editor {
  height: 200px;
}
</style>
